package com.creating.mict.common;



import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.Calendar;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;

import com.sun.net.httpserver.HttpContext;

public class Files {

	public void exportFile(HttpServletResponse response,
			HttpServletRequest request, File file, boolean isDel)
			throws IOException {

		OutputStream out = null;
		InputStream in = null;
		// 获得文件名
		String filename = URLEncoder.encode(file.getName(), "UTF-8");
		// 定义输出类型(下载)
		response.setContentType("application/force-download");
		response.setHeader("Location", filename);
		// 定义输出文件头
		response.setHeader("Content-Disposition", "attachment;filename="
				+ filename);
		out = response.getOutputStream();
		in = new FileInputStream(file.getPath());

		byte[] buffer = new byte[1024];
		int i = -1;
		while ((i = in.read(buffer)) != -1) {
			out.write(buffer, 0, i);
		}

		in.close();
		out.close();

		if (isDel) {
			// 删除文件,删除前关闭所有的Stream.
			file.delete();
		}
	}
	
	public void uploadFile(HttpServletRequest request, File file, String fileName,
			boolean isDel) throws IOException {
		
		// 首先判断用户是否曾经上传过,如果上传过,则删掉原来的文件(这里我没考虑其他情况,考虑周全还要写一些代码)
		String oldFileName = request.getParameter("upload");
		// 如果存在则删除
		if (!"noFile".equalsIgnoreCase(oldFileName)) {
			File oldFile = new File(ServletActionContext.getServletContext()
					.getRealPath("/") + "uploadFiles/" + oldFileName);
			oldFile.delete();
		}

		// 为用户上传的文件新取一个名字
		Calendar day = Calendar.getInstance();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
		String newName = sdf.format(day.getTime());
		// 获取用户上传的文件格式
		String exp = getFileExp(fileName);

		// 将文件写入文件夹
		FileOutputStream fos = new FileOutputStream(ServletActionContext
				.getServletContext().getRealPath("/")
				+ "uploadFiles/"
				+ newName + exp);

		FileInputStream fis = new FileInputStream(file);
		byte[] buffer = new byte[10240];
		int len = 0;

		// int total = fis.available(); // 大小
		while ((len = fis.read(buffer)) > 0) {
			fos.write(buffer, 0, len);
			fos.flush();
		}
		fis.close();
		fos.close();
		
	}
	
	// 获取文件格式(扩展名)
	private String getFileExp(String name) {
		int pos = name.lastIndexOf(".");
		return name.substring(pos);
	}

	// 删除文件夹以及文件夹下的子目录与文件
	public static void deleteFile(File file) {
		if (file.exists()) {
			if (file.isFile()) {
				file.delete();
			} else if (file.isDirectory()) {
				File files[] = file.listFiles();
				for (int i = 0; i < files.length; i++) {
					deleteFile(files[i]);
				}
			}
			file.delete();
		} else {
			System.out.println("所删除的文件不存在！" + '\n');
		}
	}
	
	public static void main(String[] args) {
		File file = new File("d:\\tmp");
		deleteFile(file);
		
	}
	
	
}
